package org.abratuhi.kvngroupware.gui;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.ScenarioManagerController;
import org.abratuhi.kvngroupware.model.Joke;
import org.abratuhi.kvngroupware.model.Scenario;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUIScenarioManager extends JPanel{
	
	public final static String AC_ADD_SCENARIO_JOKE = "add_scenario_joke";
	public final static String AC_REMOVE_SCENARIO_JOKE = "remove_scenario_joke";
	public final static String AC_UP_SCENARIO_JOKE = "up_scenario_joke";
	public final static String AC_DOWN_SCENARIO_JOKE = "down_scenario_joke";
	public final static String AC_SAVE_SCENARIO = "save_scenario";
	
	String[] tableColumns = new String[]{};
	DefaultTableModel modelJokes = new DefaultTableModel();
	DefaultTableModel modelScenario = new DefaultTableModel();
	public JTable tableJokes = new JTable(modelJokes);
	public JTable tableScenario = new JTable(modelScenario);
	public JTextArea textJoke = new JTextArea();
	
	JButton buttonAddJoke = new JButton("→");
	JButton buttonRemoveJoke = new JButton("←");
	JButton buttonUpJoke = new JButton("↑");
	JButton buttonDownJoke = new JButton("↓");
	
	JFrame frame;
	
	public KVNGroupwareClient kvnclient;
	public ScenarioManagerController contoller;
	public Scenario scenario;
	public ArrayList<Joke> jokes;
	public Joke selectedJoke;
	
	public GUIScenarioManager(KVNGroupwareClient kvnclient, Scenario scenario, Properties locale){
		super();
		
		this.kvnclient = kvnclient;
		this.scenario = scenario;
		this.jokes = kvnclient.getJokes();
		this.contoller = new ScenarioManagerController(this.kvnclient, this, this.scenario, this.jokes);
		
		this.tableColumns = locale.getProperty("scenario.columns").split(",");
		
		for(int i=0; i<tableColumns.length; i++) modelJokes.addColumn(tableColumns[i]);
		tableJokes.getColumnModel().getColumn(0).setPreferredWidth(50);
		tableJokes.getColumnModel().getColumn(0).setMaxWidth(50);
		tableJokes.getColumnModel().getColumn(1).setMinWidth(200);
		tableJokes.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		for(int i=0; i<tableColumns.length; i++) modelScenario.addColumn(tableColumns[i]);
		tableScenario.getColumnModel().getColumn(0).setPreferredWidth(50);
		tableScenario.getColumnModel().getColumn(0).setMaxWidth(50);
		tableScenario.getColumnModel().getColumn(1).setMinWidth(200);
		tableScenario.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		textJoke.setColumns(20);
		textJoke.setRows(6);
		
		buttonAddJoke.setActionCommand(AC_ADD_SCENARIO_JOKE);
		buttonRemoveJoke.setActionCommand(AC_REMOVE_SCENARIO_JOKE);
		buttonUpJoke.setActionCommand(AC_UP_SCENARIO_JOKE);
		buttonDownJoke.setActionCommand(AC_DOWN_SCENARIO_JOKE);
		//buttonSaveScenario.setActionCommand(AC_SAVE_SCENARIO);
		
		buttonAddJoke.addActionListener(contoller);
		buttonRemoveJoke.addActionListener(contoller);
		buttonUpJoke.addActionListener(contoller);
		buttonDownJoke.addActionListener(contoller);
		//buttonSaveScenario.addActionListener(contoller);
		
		tableJokes.getSelectionModel().addListSelectionListener(contoller);
		tableScenario.getSelectionModel().addListSelectionListener(contoller);
		
		Box boxB1 = Box.createVerticalBox();
		boxB1.add(Box.createGlue());
		boxB1.add(buttonAddJoke);
		boxB1.add(buttonRemoveJoke);
		boxB1.add(Box.createGlue());
		
		JPanel panelLU = new JPanel();
		panelLU.setLayout(new SpringLayout());
		panelLU.add(new JScrollPane(tableJokes));
		panelLU.add(boxB1);
		SpringUtilities.makeCompactGrid(panelLU, 1, 2, 0, 0, 0, 0);
		
		JPanel panelL = new JPanel();
		panelL.setLayout(new SpringLayout());
		panelL.add(panelLU);
		panelL.add(new JScrollPane(textJoke));
		SpringUtilities.makeCompactGrid(panelL, 2, 1, 0, 0, 0, 0);
		
		Box boxB2 = Box.createVerticalBox();
		boxB2.add(Box.createGlue());
		boxB2.add(buttonUpJoke);
		boxB2.add(buttonDownJoke);
		boxB2.add(Box.createGlue());
		
		JPanel panelRU = new JPanel();
		panelRU.setLayout(new SpringLayout());
		panelRU.add(new JScrollPane(tableScenario));
		panelRU.add(boxB2);
		SpringUtilities.makeCompactGrid(panelRU, 1, 2, 0, 0, 0, 0);
		
		JPanel panelR = new JPanel();
		panelR.setLayout(new SpringLayout());
		panelR.add(panelRU);
		SpringUtilities.makeCompactGrid(panelR, 1, 1, 0, 0, 0, 0);
		
		JSplitPane splitV = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelL, panelR);
		
		this.setLayout(new SpringLayout());
		this.add(splitV);
		SpringUtilities.makeCompactGrid(this, 1, 1, 0, 0, 0, 0);
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		// jokes
		int nrows = modelJokes.getRowCount();
		for(int i=0; i<nrows; i++){
			modelJokes.removeRow(0);
		}
		for(int i=0; i<jokes.size(); i++){
			modelJokes.addRow(new String[]{jokes.get(i).getRating(), jokes.get(i).getText()});
		}
		if(selectedJoke != null) tableJokes.getSelectionModel().setLeadSelectionIndex(kvnclient.indexOf(selectedJoke));
		
		// joke
		if(selectedJoke != null) textJoke.setText(selectedJoke.getText());
		
		// scenario
		nrows = modelScenario.getRowCount();
		for(int i=0; i<nrows; i++){
			modelScenario.removeRow(0);
		}
		for(int i=0; i<scenario.size(); i++){
			Joke joke = kvnclient.getJoke(scenario.get(i));
			modelScenario.addRow(new String[]{joke.getRating(), joke.getText()});
		}
		if(selectedJoke != null) tableScenario.getSelectionModel().setLeadSelectionIndex(scenario.indexOf(selectedJoke.getId()));
	}
	
	public void createJFrame(){
		frame = new JFrame("..:: KVN Groupware Client ::..");
		frame.getContentPane().add(this);
		frame.setVisible(true);
		frame.addWindowListener(contoller);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}



	/*
	public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
		public MyComboBoxRenderer(String[] items) {
			super(items);
		}

		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			if (isSelected) {
				setForeground(table.getSelectionForeground());
				super.setBackground(table.getSelectionBackground());
			} else {
				setForeground(table.getForeground());
				setBackground(table.getBackground());
			}

			// Select the current value
			setSelectedItem(value);
			return this;
		}
	}

	public class MyComboBoxEditor extends DefaultCellEditor {
		public MyComboBoxEditor(String[] items) {
			super(new JComboBox(items));
		}
	}*/
	/*
	public static void main(String [] args){
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		
		JTable table = new JTable();
	    DefaultTableModel model = (DefaultTableModel)table.getModel();
	    
	    // Add some columns
	    model.addColumn("A", new Object[]{"item1"});
	    model.addColumn("B", new Object[]{"item2"});
	    
	    // These are the combobox values
	    String[] values = new String[]{"item1", "item2", "item3"};
	    
	    // Set the combobox editor on the 1st visible column
	    int vColIndex = 0;
	    TableColumn col = table.getColumnModel().getColumn(vColIndex);
	    col.setCellEditor(new GUIScenarioManager().new MyComboBoxEditor(values));
	    
	    // If the cell should appear like a combobox in its
	    // non-editing state, also set the combobox renderer
	    col.setCellRenderer(new GUIScenarioManager().new MyComboBoxRenderer(values));
	    
	    f.add(table);
	    f.pack();
	}
	*/

}
